package com.tengju.bff.interfaces.app.user;

import com.tengju.bff.interfaces.app.user.assembler.StarSignInfoAssembler;
import com.tengju.bff.interfaces.app.user.facade.StarSignFacade;
import com.tengju.bff.interfaces.manage.star.vo.StarExamResultVO;
import com.tengju.bff.interfaces.manage.star.vo.StarSignInfoVO;
import com.tengju.bff.interfaces.manage.star.vo.SurveyConfigVO;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.InterfaceException;
import com.tengju.bff.interfaces.shared.SystemException;
import com.tengju.bff.interfaces.shared.servlet.ServletContextUtil;
import com.tengju.bff.interfaces.shared.servlet.UserServletContext;
import com.tengju.user.application.archives.StarSignService;
import com.tengju.user.application.archives.dto.SurveyContentDTO;
import com.tengju.user.domain.model.sign.StarAccountEvent;
import com.tengju.user.domain.model.sign.StarExamResultEnum;
import com.tengju.user.domain.model.sign.StarSignInfo;
import com.tengju.user.domain.model.sign.StarSignInfoRepository;
import com.tengju.user.domain.model.survey.SurveyConfig;
import com.tengju.user.domain.model.survey.SurveyContent;
import com.tengju.user.domain.model.survey.SurveyRepotiory;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.domain.shared.ResultCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import static com.tengju.user.domain.shared.ResultCodeEnum.SYS_EXCEPTION;
import static com.tengju.user.domain.shared.ResultCodeEnum.SYS_MSG;

@Validated
@RestController
@Api(tags = "星级签约信息")
@RequestMapping(value = "app/archives/star/sign")
@Slf4j
public class StarSignController {

    @Autowired
    StarSignInfoRepository starSignInfoRepository;

    @Autowired
    StarSignService starSignService;

    @Autowired
    SurveyRepotiory surveyRepotiory;

    @Autowired
    StarSignFacade starSignFacade;

    @Value("${app.sync.cdn.success}")
    private Boolean syncCdnSuccess;


    @ApiOperation(value = "获取星级签约信息", notes = "获取星级签约信息")
    @RequestMapping(value = "getUserStarSignInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<StarSignInfoVO> getUserStarSignInfo() {
        try {
            UserServletContext userServletContext = ServletContextUtil.getServletContext();
            UserIdCode userIdCode = userServletContext.getIdCode();
            if(userIdCode == null){
                return ApiResponse.newSuccess(null);
            }

            //2.0关闭签约入口
//            if (StringUtils.isEmpty(userServletContext.getWeexVersion())) {
//                if (syncCdnSuccess) {
//                    return ApiResponse.newFailure(ResultCodeEnum.TOKEN_ILLEGAL);
//                }
//                return ApiResponse.newSuccess();
//            }

            StarSignInfo starSignInfo = starSignInfoRepository.getByIdCode(userIdCode);
            return ApiResponse.newSuccess(StarSignInfoAssembler.signInfoToVO(starSignInfo));
        } catch (SystemException e) {
            return ApiResponse.newFailure(e.getErrorCode(), e.getErrorMessage());
        }
    }


    @ApiOperation(value = "DT用户生成星级资质审核数据", notes = "DT用户生成星级资质审核数据")
    @RequestMapping(value = "addAuditData", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<StarSignInfoVO> addAuditDataFromApp(@RequestParam Integer accountType) {
        try {
            UserServletContext userServletContext = ServletContextUtil.getServletContext();
            long idCode = userServletContext.needIdCode().getValue();
            StarAccountEvent event = new StarAccountEvent(
                    UserIdCode.of(idCode),
                    accountType,
                    null
            );
            StarSignInfo starSignInfo = starSignService.addAuditData(event);
            return ApiResponse.newSuccess(StarSignInfoAssembler.signInfoToVO(starSignInfo));
        } catch (InterfaceException e) {
            return ApiResponse.newFailure(e.getCode(), e.getMsg());
        }
    }

    @ApiOperation(value = "完成星级签约", notes = "完成星级签约")
    @RequestMapping(value = "finishSign", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> addAuditDataFromApp() {
        try {
            UserServletContext userServletContext = ServletContextUtil.getServletContext();
            UserIdCode idCode = userServletContext.needIdCode();
            starSignService.finishSign(idCode);
            return ApiResponse.newSuccess(true);
        } catch (InterfaceException e) {
            return ApiResponse.newFailure(e.getCode(), e.getMsg());
        }
    }

    @ApiOperation(value = "获取调查问卷配置", notes = "获取调查问卷配置")
    @RequestMapping(value = "getSurveyConfig", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<SurveyConfigVO> getSurveyConfig(@NotNull @RequestParam Long surveyId) {
        try {
            SurveyConfig surveyConfig = surveyRepotiory.getConfigById(surveyId);
            return ApiResponse.newSuccess(StarSignInfoAssembler.surveyConfigToVO(surveyConfig));
        } catch (SystemException e) {
            return ApiResponse.newFailure(e.getErrorCode(), e.getErrorMessage());
        }
    }

    @ApiOperation(value = "提交调查问卷", notes = "提交调查问卷")
    @RequestMapping(value = "submitSurvey", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> submitSurvey(@Valid @RequestBody SurveyContentDTO surveyContentDTO, BindingResult bindingResult) {
        try {
            UserServletContext userServletContext = ServletContextUtil.getServletContext();
            long idCode = userServletContext.needIdCode().getValue();
            surveyContentDTO.setIdCode(idCode);
            SurveyContent surveyContent = StarSignInfoAssembler.surveyContentDTOToEntity(surveyContentDTO);
            starSignService.submitSurveyContent(surveyContent);
            return ApiResponse.newSuccess(true);
        } catch (InterfaceException e) {
            return ApiResponse.newFailure(e.getCode(), e.getMsg());
        }
    }

    @ApiOperation(value = "提交星级考试", notes = "提交星级考试")
    @RequestMapping(value = "submitStarExam", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<StarExamResultVO> submitStarExam(@Valid @RequestBody SurveyContentDTO surveyContentDTO, BindingResult bindingResult) {
        try {
            UserServletContext userServletContext = ServletContextUtil.getServletContext();
            UserIdCode idCode = userServletContext.needIdCode();
            surveyContentDTO.setIdCode(idCode.getValue());
            SurveyContent surveyContent = StarSignInfoAssembler.surveyContentDTOToEntity(surveyContentDTO);
            StarExamResultVO starExamResultVO = starSignFacade.submitStarExam(surveyContent);
            if (starExamResultVO.getExamResult() == StarExamResultEnum.PASS.getCode()) {
                starSignService.passExam(idCode);
            }
            return ApiResponse.newSuccess(starExamResultVO);
        } catch (InterfaceException e) {
            return ApiResponse.newFailure(e.getCode(), e.getMsg());
        }
    }

}
